
Public Class BiochemistryAbnormalStatusHistory

    Protected _year As String

    Protected _police As New Police()
    Protected _biochemistry_abnormal As New BiochemistryAbnormal()

    Public Property police() As Police
        Get
            Return _police
        End Get
        Set(ByVal value As Police)
            _police = value
        End Set
    End Property

    Public Property year() As String
        Get
            Return _year
        End Get
        Set(ByVal value As String)
            _year = value
        End Set
    End Property

    Public Property biochemistry_abnormal() As BiochemistryAbnormal
        Get
            Return _biochemistry_abnormal
        End Get
        Set(ByVal value As BiochemistryAbnormal)
            _biochemistry_abnormal = value
        End Set
    End Property

    Public Sub deletePoliceBiochemistryResultAbnormalStatus(ByVal inBiochemistryResultHistory As BiochemistryResultHistory)
        Try
            Dim decryptText As New DateUtilities.Encrypt_Data()
            Dim objConn As New MySql.Data.MySqlClient.MySqlConnection( _
                decryptText.getDecrypt(System.Configuration.ConfigurationManager.ConnectionStrings( _
                "PoliceHealthConnectionString").ToString(), True))
            Dim objCmd As New MySql.Data.MySqlClient.MySqlCommand()
            objCmd.Connection = objConn
            objCmd.CommandType = CommandType.StoredProcedure
            objCmd.CommandText = "proc_DELETE_PoliceBiochemistryAbnormalStatusHistory"

            Dim objParam As New MySql.Data.MySqlClient.MySqlParameter("inCitizenID", inBiochemistryResultHistory.police.citizen_id)
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inYear", inBiochemistryResultHistory.year)
            objCmd.Parameters.Add(objParam)

            objConn.Open()
            objCmd.ExecuteNonQuery()
            objConn.Close()

        Catch ex As Exception
            Throw ex
        End Try
    End Sub

    Public Function insertPoliceBiochemistryAbnormalStatusHistory _
        (ByVal inBiochemistryResultHistory As BiochemistryResultHistory) As Boolean
        Dim blResultStatus As Boolean = True
        For i As Integer = 1 To 10
            blResultStatus = blResultStatus And insertPoliceBiochemistryAbnormalStatusHistory(inBiochemistryResultHistory, i)
        Next
        Return blResultStatus
    End Function

    Public Function insertPoliceBiochemistryAbnormalStatusHistory _
        (ByVal inBiochemistryResultHistory As BiochemistryResultHistory, ByVal inLoop As Short) As Boolean
        Try
            Dim decryptText As New DateUtilities.Encrypt_Data()
            Dim objConn As New MySql.Data.MySqlClient.MySqlConnection( _
                decryptText.getDecrypt(System.Configuration.ConfigurationManager.ConnectionStrings( _
                "PoliceHealthConnectionString").ToString(), True))
            Dim objCmd As New MySql.Data.MySqlClient.MySqlCommand()
            objCmd.Connection = objConn
            objCmd.CommandType = CommandType.StoredProcedure
            objCmd.CommandText = "proc_INSERT_PoliceBiochemistryAbnormalStatusHistory"

            Dim objParam As New MySql.Data.MySqlClient.MySqlParameter("inCitizenID", inBiochemistryResultHistory.police.citizen_id)
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inYear", inBiochemistryResultHistory.year)
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inOrganizationID", _
                Utilities.SendToDatabase(inBiochemistryResultHistory.police.organization.organization_id))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inOrganizationNameTha", _
                Utilities.SendToDatabase(inBiochemistryResultHistory.police.organization.organization_name_tha))
            objCmd.Parameters.Add(objParam)

            objParam = New MySql.Data.MySqlClient.MySqlParameter("inCreateUser", inBiochemistryResultHistory.police.create_user)
            objCmd.Parameters.Add(objParam)

            Select Case inLoop
                Case 1
                    If inBiochemistryResultHistory.glucose >= 126 And _
                    Not IsDBNull(Utilities.SendToDatabase(inBiochemistryResultHistory.glucose)) Then
                        objParam = New MySql.Data.MySqlClient.MySqlParameter("inBiochemistryAbnormalID", 2)
                        objCmd.Parameters.Add(objParam)

                        objConn.Open()
                        objCmd.ExecuteNonQuery()
                        objConn.Close()

                        Return False
                    ElseIf (inBiochemistryResultHistory.glucose >= 101 And inBiochemistryResultHistory.glucose <= 125) And _
                    Not IsDBNull(Utilities.SendToDatabase(inBiochemistryResultHistory.glucose)) Then
                        objParam = New MySql.Data.MySqlClient.MySqlParameter("inBiochemistryAbnormalID", 1)
                        objCmd.Parameters.Add(objParam)

                        objConn.Open()
                        objCmd.ExecuteNonQuery()
                        objConn.Close()

                        Return False
                    End If
                Case 2
                    If (inBiochemistryResultHistory.crea > 1.5 Or inBiochemistryResultHistory.urea > 23) And _
                    Not IsDBNull(Utilities.SendToDatabase(inBiochemistryResultHistory.crea)) And _
                    Not IsDBNull(Utilities.SendToDatabase(inBiochemistryResultHistory.urea)) Then
                        objParam = New MySql.Data.MySqlClient.MySqlParameter("inBiochemistryAbnormalID", 3)
                        objCmd.Parameters.Add(objParam)

                        objConn.Open()
                        objCmd.ExecuteNonQuery()
                        objConn.Close()

                        Return False
                    End If
                Case 3
                    If inBiochemistryResultHistory.uric_acid > 7 And _
                    Not IsDBNull(Utilities.SendToDatabase(inBiochemistryResultHistory.uric_acid)) Then
                        objParam = New MySql.Data.MySqlClient.MySqlParameter("inBiochemistryAbnormalID", 4)
                        objCmd.Parameters.Add(objParam)

                        objConn.Open()
                        objCmd.ExecuteNonQuery()
                        objConn.Close()

                        Return False
                    End If
                Case 4
                    If inBiochemistryResultHistory.chol > 200 And _
                    Not IsDBNull(Utilities.SendToDatabase(inBiochemistryResultHistory.chol)) Then
                        objParam = New MySql.Data.MySqlClient.MySqlParameter("inBiochemistryAbnormalID", 5)
                        objCmd.Parameters.Add(objParam)

                        objConn.Open()
                        objCmd.ExecuteNonQuery()
                        objConn.Close()

                        Return False
                    End If
                Case 5
                    If inBiochemistryResultHistory.tg > 150 And _
                    Not IsDBNull(Utilities.SendToDatabase(inBiochemistryResultHistory.tg)) Then
                        objParam = New MySql.Data.MySqlClient.MySqlParameter("inBiochemistryAbnormalID", 6)
                        objCmd.Parameters.Add(objParam)

                        objConn.Open()
                        objCmd.ExecuteNonQuery()
                        objConn.Close()

                        Return False
                    End If
                Case 6
                    Select Case inBiochemistryResultHistory.police.gender
                        Case "M"
                            If inBiochemistryResultHistory.hdl < 40 And _
                            Not IsDBNull(Utilities.SendToDatabase(inBiochemistryResultHistory.hdl)) Then
                                objParam = New MySql.Data.MySqlClient.MySqlParameter("inBiochemistryAbnormalID", 7)
                                objCmd.Parameters.Add(objParam)

                                objConn.Open()
                                objCmd.ExecuteNonQuery()
                                objConn.Close()

                                Return False
                            End If
                        Case "F"
                            If inBiochemistryResultHistory.hdl < 50 And _
                            Not IsDBNull(Utilities.SendToDatabase(inBiochemistryResultHistory.hdl)) Then
                                objParam = New MySql.Data.MySqlClient.MySqlParameter("inBiochemistryAbnormalID", 7)
                                objCmd.Parameters.Add(objParam)

                                objConn.Open()
                                objCmd.ExecuteNonQuery()
                                objConn.Close()

                                Return False
                            End If
                    End Select
                Case 7
                    'Try
                    If IsNumeric(inBiochemistryResultHistory.ldl) Then
                        If CInt(inBiochemistryResultHistory.ldl) > 120 And Not IsDBNull(Utilities.SendToDatabase(inBiochemistryResultHistory.ldl)) Then
                            objParam = New MySql.Data.MySqlClient.MySqlParameter("inBiochemistryAbnormalID", 8)
                            objCmd.Parameters.Add(objParam)

                            objConn.Open()
                            objCmd.ExecuteNonQuery()
                            objConn.Close()

                            Return False
                            'Else
                            '    Return True
                        End If
                    End If
                    'Integer.Parse(inBiochemistryResultHistory.ldl)
                    'If CInt(inBiochemistryResultHistory.ldl) < 120 And Not IsDBNull(Utilities.SendToDatabase(inBiochemistryResultHistory.ldl)) Then
                    '    objParam = New MySql.Data.MySqlClient.MySqlParameter("inBiochemistryAbnormalID", 8)
                    '    objCmd.Parameters.Add(objParam)

                    '    objConn.Open()
                    '    objCmd.ExecuteNonQuery()
                    '    objConn.Close()

                    '    Return False
                    'Else
                    '    Return True
                    'End If
                    '        Catch ex As Exception
                    'Throw ex
                    'objParam = New MySql.Data.MySqlClient.MySqlParameter("inBiochemistryAbnormalID", 8)
                    'objCmd.Parameters.Add(objParam)

                    'objConn.Open()
                    'objCmd.ExecuteNonQuery()
                    'objConn.Close()

                    'Return False
                    'End Try
                Case 8
                    If (inBiochemistryResultHistory.sgol > (40 * 2) Or inBiochemistryResultHistory.sgpt > (40 * 2)) And _
                    Not IsDBNull(Utilities.SendToDatabase(inBiochemistryResultHistory.sgol)) And _
                    Not IsDBNull(Utilities.SendToDatabase(inBiochemistryResultHistory.sgpt)) Then
                        objParam = New MySql.Data.MySqlClient.MySqlParameter("inBiochemistryAbnormalID", 9)
                        objCmd.Parameters.Add(objParam)

                        objConn.Open()
                        objCmd.ExecuteNonQuery()
                        objConn.Close()

                        Return False
                    End If
                Case 9
                    Select Case inBiochemistryResultHistory.alkaline_phos_liquid.alkaline_phos_liquid_id
                        Case 1
                            If inBiochemistryResultHistory.alkaline_phos > 129 And _
                            Not IsDBNull(Utilities.SendToDatabase(inBiochemistryResultHistory.alkaline_phos)) Then
                                objParam = New MySql.Data.MySqlClient.MySqlParameter("inBiochemistryAbnormalID", 10)
                                objCmd.Parameters.Add(objParam)

                                objConn.Open()
                                objCmd.ExecuteNonQuery()
                                objConn.Close()

                                Return False
                            End If
                        Case 2
                            If inBiochemistryResultHistory.alkaline_phos > 306 And _
                            Not IsDBNull(Utilities.SendToDatabase(inBiochemistryResultHistory.alkaline_phos)) Then
                                objParam = New MySql.Data.MySqlClient.MySqlParameter("inBiochemistryAbnormalID", 10)
                                objCmd.Parameters.Add(objParam)

                                objConn.Open()
                                objCmd.ExecuteNonQuery()
                                objConn.Close()

                                Return False
                            End If
                    End Select
                Case 10
                    Select Case inBiochemistryResultHistory.lead_person.lead_person_id
                        Case 1
                            If inBiochemistryResultHistory.lead > 30 And _
                            Not IsDBNull(Utilities.SendToDatabase(inBiochemistryResultHistory.lead)) Then
                                objParam = New MySql.Data.MySqlClient.MySqlParameter("inBiochemistryAbnormalID", 11)
                                objCmd.Parameters.Add(objParam)

                                objConn.Open()
                                objCmd.ExecuteNonQuery()
                                objConn.Close()

                                Return False
                            End If
                        Case 2
                            If inBiochemistryResultHistory.lead > 60 And _
                            Not IsDBNull(Utilities.SendToDatabase(inBiochemistryResultHistory.lead)) Then
                                objParam = New MySql.Data.MySqlClient.MySqlParameter("inBiochemistryAbnormalID", 11)
                                objCmd.Parameters.Add(objParam)

                                objConn.Open()
                                objCmd.ExecuteNonQuery()
                                objConn.Close()

                                Return False
                            End If
                    End Select
            End Select
        Catch ex As Exception
            Dim msgWarnning As New ManageBiochemistryResultHistoryControl()
            msgWarnning.showInsertBiochemistryAbnormalStatusHistoryError()
        End Try
        Return True
    End Function


End Class
